<template>
    <div class="insert-contis">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/warehouse/pandian' }">盘点仓库列表</el-breadcrumb-item>
            <el-breadcrumb-item>新增仓库</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-form ref="formRef" :model="form" label-width="120px" :rules="formRules">
                <el-form-item label="输入盘点单号" prop="didan" :rules="[{ required: true, message: '输入盘点单号' }]">
                    <el-input v-model="form.didan"></el-input>
                </el-form-item>
                <el-form-item label="输入盘点名称" prop="name" :rules="[{ required: true, message: '输入盘点名称' }]">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="仓库ID" prop="typediaoru" :rules="[{ required: true, message: '请选择仓库ID' }]">
                    <el-select v-model="form.typediaoru" placeholder="选择仓库ID">
                        <el-option v-for="warehouse in warehouseList" :key="warehouse.warehouseId"
                            :label="warehouse.warehouseId" :value="warehouse.warehouseId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="盘点类型" prop="diaoboleix" :rules="[{ required: true, message: '请选择类型' }]">
                    <el-select v-model="form.diaoboleix" placeholder="选择盘点类型">
                        <el-option v-for="item in diaoboleixs" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="计划开始时间" prop="date" :rules="[{ required: true, message: '请选择日期' }]">
                    <el-date-picker v-model="form.date" type="date">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="实际开始时间" prop="kaishidate" :rules="[{ required: true, message: '请选择日期' }]">
                    <el-date-picker v-model="form.date" type="kaishidate">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="实际结束时间" prop="jieshudate" :rules="[{ required: true, message: '请选择日期' }]">
                    <el-date-picker v-model="form.date" type="jieshudate">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="状态" prop="zhuangtai" :rules="[{ required: true, message: '请选择状态' }]">
                    <el-select v-model="form.zhuangtai" placeholder="选择状态">
                        <el-option value="1" label="1-待处理"></el-option>
                        <el-option value="2" label="2-已完成"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="输入创建者" prop="cuanjianzhe" :rules="[{ required: true, message: '输入创建者' }]">
                    <el-input v-model="form.cuanjianzhe"></el-input>
                </el-form-item>
                <el-form-item v-if="false" label="创建时间" prop="cuanjshijian">
                    <el-date-picker v-model="form.cuanjshijian" type="date">
                    </el-date-picker>
                </el-form-item>
                <el-form-item v-if="false" label="更新时间" prop="gengxinshijian">
                    <el-date-picker v-model="form.gengxinshijian" type="date">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button @click="$router.back()">取消</el-button>
                    <el-button type="primary" @click="submitFrom">提交</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script setup>
import { ref, defineEmits, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import request from '@/api/request';
import { useRouter } from 'vue-router';

let emits = defineEmits(["closeDialog"])
let formRef = ref(null)

const router = useRouter();

const warehouseList = ref([]);

const diaoboleixs = ref([
    { value: "全盘", label: "全盘" },
    { value: "部盘", label: "部盘" }
])
 const loadwarehouseList = () => {
    request({
        method: "get",
        url: "/warehouse/tree",
    }).then(res => {
        console.log("仓库列表数据:", res.data);
        if (res.data && Array.isArray(res.data)) {
            warehouseList.value = res.data.filter(warehouse => warehouse.warehouseId);
        } else {
            console.error("获取仓库列表失败:", res.message);
            ElMessage.error(`仓库数据加载失败:${res.message || '无仓库数据'}`);
        }
    }).catch(err => {
        console.error("请求仓库接口异常:", err)
        ElMessage.error(`仓库数据加载异常:${err.message || '网络异常'}`)
    })
}
onMounted(() => {
    loadwarehouseList();
})
const form = ref({

    didan: '',
    name: '',
    typediaoru: '',
    diaoboleix: '',
    date: new Date(),
    kaishidate: new Date(),
    jieshudate: new Date(),
    zhuangtai: '1',
    cuanjianzhe: ''
});
const submitFrom = () => {
    formRef.value.validate((isValid) => {
        if (isValid) {
            const submitData = {
                countCode: form.value.didan,
                countName: form.value.name,
                warehouseId: form.value.typediaoru,
                countType: form.value.diaoboleix,
                plannedStartTime: form.value.date,
                actualStartTime: form.value.kaishidate,
                actualEndTime: form.value.jieshudate,
                status: form.value.zhuangtai,
                createBy: form.value.cuanjianzhe
            };

            request({
                method: "post",
                url: "/inventory/insect",
                data: submitData
            }).then(res => {
                if (res.data?.success && res.data?.countId) {
                    ElMessage.success("新增盘点单成功!");
                    router.push({
                        path: "/warehouse/pandianmingxi/insert", // 盘点明细新增页路由
                        query: { countId: res.data.countId } // 携带盘点单ID到明细页
                    });
                } else {
                    const errMsg = res.data?.message || '未知错误';
                    console.error("新增失败:", errMsg);
                    ElMessage.error(`新增失败: ${errMsg}`);
                }
            }).catch(err => {
                console.error("请求异常:", err);
                ElMessage.error(`提交失败: ${err.message || '网络错误'}`);
            });
        }
    });
};
</script>
<style scoped>
.insert-container {
    padding: 20px;
}

.el-breadcrumb {
    margin-bottom: 20px;
}
</style>
